#define VSUDT_C

#include <project.h>

#include <stdio.h>
#include <errno.h>

#include <string.h>
#include <stdlib.h>
#include <aclstr.h>
#include <ascii.h>
#include <actutil.h>
#include <aclconio.h>
#include <svc.h>

/** /#define DEBUG_VSUDT*/
#ifdef DEBUG_VSUDT
#define DEBUG_95
#endif

#include <define.h>
#include <transact.h>
#include <tran.h>
#include <gds.h>
#include <sizes.h>
#include <confio.h>

#include <cdt.h>
#include <msg.h>
#include <msgfuncs.h>
#include <msgclrk.h>
#include <fields.h>
#include <tran.h>
#include <oper.h>    /* pj1 */
#include <funcids.h>
#include <entry.h>
#include <ui.h>
#include <tct.h>
#include <spdebug.h>
#include <util.h>
#include <file.h>
#include <mit.h>
#include <menufunc.h>
#include <menus.h>
#include <clerk.h>
#include <clrkrpts.h>

#include <udt.h>
#include <mht.h>
#include <hdt.h>

static UDT_REC srUDTRec;

static int recordNum;

int inLoadUDTRec(int inRecNumber) {
    int inRet = inLoadConfRec(UDT_FILENAME, SIZE_UDT_REC, inRecNumber, (char*) &srUDTRec);

    recordNum = inRecNumber;
    return inRet;
}

int inLoadUDTRecFromStruct(int inRecNumber,void* vdStruct) {
    int inRet = inLoadConfRec(UDT_FILENAME, SIZE_UDT_REC, inRecNumber, (char*) vdStruct);

    recordNum = inRecNumber;
    return inRet;
}

int inSaveUDTRec(int inRecNumber) {
    int inRet = inSaveConfRec(UDT_FILENAME, SIZE_UDT_REC, inRecNumber, (char*) &srUDTRec);

    return inRet;
}

int inSaveUDTRecFromStruct(int inRecNumber,void* vdStruct) {
    int inRet = inSaveConfRec(UDT_FILENAME, SIZE_UDT_REC, inRecNumber, (char*) vdStruct);

    return inRet;
}

int inUpdateUDTRec(void) {
    int inRet = inSaveConfRec(UDT_FILENAME, SIZE_UDT_REC, recordNum, (char*) &srUDTRec);

    return inRet;
}

int inUpdateUDTRecFromStruct(void* vdStruct) {
    int inRet = inSaveConfRec(UDT_FILENAME, SIZE_UDT_REC, recordNum, (char*) vdStruct);

    return inRet;
}

void vdResetUDTRec(void) {
    memset((void *) &srUDTRec, 0, sizeof(srUDTRec));
}

char* szGetClerkId(void) {
    return srUDTRec.szClerkId;
}

char* szGetClerkIdFromStruct(void* vdStruct) {
    return ((UDT_REC *) vdStruct)->szClerkId;
}

void vdSetClerkId(char* szClerkId) {
    strlcpy(srUDTRec.szClerkId, szClerkId, sizeof(srUDTRec.szClerkId));
}

void vdSetClerkIdFromStruct(char* szClerkId,void* vdStruct) {
    strlcpy(((UDT_REC *) vdStruct)->szClerkId, szClerkId, sizeof(srUDTRec.szClerkId));
}

char* szGetClerkPasswd(void) {
    return srUDTRec.szClerkPasswd;
}

char* szGetClerkPasswdFromStruct(void* vdStruct) {
    return ((UDT_REC *) vdStruct)->szClerkPasswd;
}

void vdSetClerkPasswd(char* szClerkPasswd) {
    strlcpy(srUDTRec.szClerkPasswd, szClerkPasswd, sizeof(srUDTRec.szClerkPasswd));
}

void vdSetClerkPasswdFromStruct(char* szClerkPasswd,void* vdStruct) {
    strlcpy(((UDT_REC *) vdStruct)->szClerkPasswd, szClerkPasswd, sizeof(srUDTRec.szClerkPasswd));
}

int inGetLogedIn(void) {
    return srUDTRec.inLogedIn;
}

int inGetLogedInFromStruct(void* vdStruct) {
    return ((UDT_REC *) vdStruct)->inLogedIn;
}

void vdSetLogedIn(int inLogedIn) {
    srUDTRec.inLogedIn = inLogedIn;
}

void vdSetLogedInFromStruct(int inLogedIn,void* vdStruct) {
    ((UDT_REC *) vdStruct)->inLogedIn = inLogedIn;
}

long lnGetCashTip(void) {
    return srUDTRec.lnCashTip;
}

long lnGetCashTipFromStruct(void* vdStruct) {
    return ((UDT_REC *) vdStruct)->lnCashTip;
}

void vdSetCashTip(long lnCashTip) {
    srUDTRec.lnCashTip = lnCashTip;
}

void vdSetCashTipFromStruct(long lnCashTip,void* vdStruct) {
    ((UDT_REC *) vdStruct)->lnCashTip = lnCashTip;
}


/********************************************************************************************* 
* @func int | vdGetUTTFileName |
*    
* returns the corresponding UTT file name based on the host
*                  
*    Host number  3, offline - VSUTT03
*    Host number 24, online  - VSUTT24
*    NB: Extension is added by the dbfm 
* 
* @rdesc return values.
* @flag int    |  UTT filename
* @end
**********************************************************************************************/

void vdGetUTTFileName(char* pchUTTFileName) {
    /** /pdebug(("--vdGetUTTFileName--")); */

    sprintf(pchUTTFileName, "UTT%02d%02d", inGetBatchIndex(), inGetMerchantNum());
}


/********************************************************************************************* 
* @func int | inOpenUTT |
*    
* Opens/Creates the desired UTT file.
* The filename MUST NOT contain a file extension                 
*    
*  
* @rdesc return values.
* @flag int    |  VS_ERR, VS_SUCCESS
* @end
**********************************************************************************************/

int inOpenUTT(DB_FILE* psrDBFPtr,char* pchFName) {
    int inRetVal;

    /** /pdebug(("--inOpenUTT -%s-",pchFName));*/

    inRetVal = db_open(psrDBFPtr, pchFName, SIZE_UTT_KEY, O_RDWR);

    if (inRetVal < 0) /* If file is non-existant, create it */
        if (errno == ENOENT)
            inRetVal = db_open(psrDBFPtr, pchFName, SIZE_UTT_KEY, (O_CREAT | O_RDWR));

    if (inRetVal < 0)
        return(VS_ERR);

    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func int | inCloseUTT |
*    
* Closes the UDT Clerk file.  NOTE: Calling database manager opens 2 files.
*                  
*    
*  
* @rdesc return values.
* @flag int    |  VS_ERR, VS_SUCCESS
* @end
**********************************************************************************************/

int inCloseUTT(DB_FILE* psrDBFPtr) {
    /** /pdebug(("--inCloseUTT--"));*/

    if (db_close(psrDBFPtr) > 0) {
        /** /pdebug(("--inCloseUTT--Success!"));*/
        return(VS_SUCCESS);
    }

    return(VS_ERR);
}

/********************************************************************************************* 
* @func int | inUTTCompareFn |
*    
* Compare function to find correct UTT record
*
* This function will be called by the following function, as
* one of the    parameters of the db_get_key() function.
*
*  
* @rdesc return values.
* @flag int    |  VS_ERR, VS_SUCCESS
* @end
**********************************************************************************************/


int inUTTCompareFn(UTT_KEY* psrKey,UTT_SKEY* MatchKey) {
    int inRetVal;

    inRetVal = VS_SUCCESS;

    /* pdebug(("--inUTTCompareFn--"));  */

    if (inRetVal == VS_SUCCESS) {
        /* Search on Clerk Id */
        if (MatchKey->inUTTKeyIdx & KEY_SVR_ID) {
            if (strcmp(MatchKey->uttKey.szClerkID, psrKey->szClerkID) != 0)
                inRetVal = VS_ERR;
        }
    }

    /* pdebug(("inRetVal = %d",inRetVal));  */
    return(inRetVal);
}

/********************************************************************************************* 
* @func int | inKeyReadUTT |
*    
* Read through the keyed UTT file to find the selected record
*
*  Before Calling this function ensure the following:
*                 1. Fill the srKey struct with relevant values
*                 2. Set the global 'inSearchIndex' variable to the search
*                    field index.
* 
*
*  
* @rdesc return values.
* @flag int    |  VS_ERR, VS_SUCCESS
* @end
**********************************************************************************************/

int inKeyReadUTT(DB_FILE* psrDBFPtr,UTT_REC* psrRec,UTT_KEY* psrKey,int inKeyIdx,long* plnRecNum) {
    long lnRetVal;
    long lnDBWhat = DB_FIRST;
    UTT_SKEY srUTTSKey;

    /** /pdebug(("--inKeyReadUTT--")); */

    /*
     *      Load Global Key Values used by inUTTCompFn
     */
    srUTTSKey.compare_func = inUTTCompareFn;
    srUTTSKey.inUTTKeyIdx = inKeyIdx;
    memcpy(&srUTTSKey.uttKey, psrKey, SIZE_UTT_KEY);
    lnRetVal = db_get_key(psrDBFPtr, psrKey, &srUTTSKey, lnDBWhat, (char *) psrRec, SIZE_UTT_REC, plnRecNum);

    if (lnRetVal == 0L) {
        memset(psrRec, '\0', SIZE_UTT_REC);
        return (VS_ERR);
    } else if (lnRetVal == -1L)
        return (VS_ESCAPE);
    return (VS_SUCCESS);
}

/********************************************************************************************* 
* @func int | inGetKeyUTTRec |
*    
* Find selected UTT record
*
*    
* @rdesc return values.
* @flag int    |  VS_ERR, VS_SUCCESS, VS_ESCAPE 
* @end
**********************************************************************************************/

int inGetKeyUTTRec(DB_FILE* psrDBFPtr,UTT_REC* psrRec,UTT_KEY* psrKey,int inKeyIdx,long lnDBWhat) {
    long lnRetVal,
    lnRecNum = 0L;
    UTT_SKEY srUTTSKey;

    /** /pdebug(("--inGetKeyUTTRec--")); */

    /*
     *      Load Global Key Values used by inUTTCompFn
     */
    srUTTSKey.compare_func = inUTTCompareFn;
    srUTTSKey.inUTTKeyIdx = inKeyIdx;
    memcpy(&srUTTSKey.uttKey, psrKey, sizeof(UTT_KEY));
    lnRetVal = db_get_key(psrDBFPtr, psrKey, &srUTTSKey, lnDBWhat, (char *) psrRec, SIZE_UTT_REC, &lnRecNum);

    if (lnRetVal == 0L) {
        memset(psrRec, '\0', SIZE_UTT_REC);
        return (VS_ERR);
    } else if (lnRetVal == -1L)
        return (VS_ESCAPE);
    return (VS_SUCCESS);
}

/********************************************************************************************* 
* @func int | inInsertUDTEntry |
*    
* Writes the new record - Clerk Id and password as a new record
*                          in sorted order. This version uses a numeric sort
*
*    
* @rdesc return values.
* @flag int    |  VS_FAILURE, VS_SUCCESS
* @end
**********************************************************************************************/

int inInsertUDTEntry(char* pchClerkId,char* pchPwd,void* psrUDTRec) {
    int inRetVal = VS_FAILURE;
    unsigned char ucUDTRec[SIZE_UDT_REC];
    FHANDLE hUDTFHandle;
    int inRecordSize;
    char szPadNewClkId[CLERK_ID_SIZE + 1];
    char szPadCurrentClkId[CLERK_ID_SIZE + 1];
    BOOL fQuitLoop = FALSE;

    strcpy(szPadNewClkId, pchClerkId);

    /* numeric sort */
    pad(szPadNewClkId, szPadNewClkId, '0', CLERK_ID_SIZE, RIGHT);

    if ((hUDTFHandle = open(UDT_FILENAME, O_RDWR)) >= 0) {
        if (lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET) == (long) GEN_VER_SIZE) {
            do {
                inRecordSize = read(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC);
                if (inRecordSize == SIZE_UDT_REC) {
                    strcpy(szPadCurrentClkId, szGetClerkIdFromStruct(ucUDTRec));

                    /* alphanumeric sort */
                    /* pad (szPadCurrentClkId, szPadCurrentClkId,SPACE, CLERK_ID_SIZE, LEFT);  */

                    /* numeric sort */
                    pad(szPadCurrentClkId, szPadCurrentClkId, '0', CLERK_ID_SIZE, RIGHT);

                    /* if new ClerkId is < ClerkId then we need to backup */
                    if (strcmp(szPadNewClkId, szPadCurrentClkId) < 0) {
                        lseek(hUDTFHandle, ((long) SIZE_UDT_REC) * -1, SEEK_CUR);
                        fQuitLoop = TRUE;
                    }
                } else
                    fQuitLoop = TRUE;
            } while (fQuitLoop != TRUE);

            vdSetClerkIdFromStruct(pchClerkId, ucUDTRec);
            vdSetClerkPasswdFromStruct(pchPwd, ucUDTRec);
            vdSetLogedInFromStruct(inGetLogedInFromStruct(psrUDTRec), ucUDTRec);
            vdSetCashTipFromStruct(lnGetCashTipFromStruct(psrUDTRec), ucUDTRec);

            if (insert(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC) == SIZE_UDT_REC)
                inRetVal = VS_SUCCESS;
            else
                inRetVal = VS_ERROR;
        } else
            inRetVal = VS_ERROR;
    } else
        inRetVal = VS_ERROR;

    close(hUDTFHandle);
    return(inRetVal);
}

/********************************************************************************************* 
* @func int | inDeleteUDTEntry |
*  Deletes a ClerkId/Password combination from UDT, and also re-arranges the
*  UDT file to avoid holes;  
* 
*    
* @rdesc return values.
* @flag int    |  VS_ERR, VS_SUCCESS, 
*                 VS_ABORT   - If only one record exists
* @end
**********************************************************************************************/

int inDeleteUDTEntry(char* pchClerkId,char* pchPwd) {
    int inResult;
    unsigned char ucUDTRec[SIZE_UDT_REC];
    FHANDLE hUDTFHandle;
    char szManagerPwd[PASSWORD_SIZE + 1];   /* pj1 - allow manager pwd */

    hUDTFHandle = open(UDT_FILENAME, O_RDWR);

#ifdef FCHK
    if (hUDTFHandle < 0)
        return(VS_ERR);
    /* Skip the gendata version information */
    if (lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET) != (long) GEN_VER_SIZE) {
        close(hUDTFHandle);
        return(VS_ERR);
    }
#else
    lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET);
#endif /* FCHK */

    do {
        inResult = read(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC);
        if (inResult != SIZE_UDT_REC)            /* Read error, get out */ {
            close(hUDTFHandle);
            return(VS_ERR);
        }
        /* Compare for a match */

        strcpy(szManagerPwd, szGetManagerPwd());       /* pj1 - allow manager pwd */
        if ((strcmp(pchClerkId, szGetClerkIdFromStruct(ucUDTRec)) == 0) && ((strcmp(pchPwd, szGetClerkPasswdFromStruct(ucUDTRec)) == 0) || (strcmp(pchPwd, szManagerPwd) == 0))) {
            /* move back file ptr and delete the record */
            if (lseek(hUDTFHandle, ((long) SIZE_UDT_REC) * -1, SEEK_CUR) == -1) {
                close(hUDTFHandle);
                return(VS_ERR);
            } /* seek error */

            if (delete(hUDTFHandle, SIZE_UDT_REC) < 0)   /* delete error */ {
                close(hUDTFHandle);
                return(VS_ERR);
            }
            close(hUDTFHandle);
            return(VS_SUCCESS);
        } else if ((strcmp(pchClerkId, szGetClerkIdFromStruct(ucUDTRec)) == 0) && !((strcmp(pchPwd, szGetClerkPasswdFromStruct(ucUDTRec)) == 0) || (strcmp(pchPwd, szGetClerkPasswdFromStruct(ucUDTRec)) == 0))) {
            vdDisplayMessageFromFile(INV_PWD_ERR_MSG21, WARNING_MSG);
            return(VS_FAILURE);
        }
    } while (1);
}

/********************************************************************************************* 
* @func int | inGetUDTPwd |
*  
* Given a clerk Id gets the password. 
*
*    
* @rdesc return values.
* @flag int    |  VS_ERR if match not found, VS_SUCCESS, 
*                 VS_FAILURE if file errors
* @end
**********************************************************************************************/

int inGetUDTPwd(char* pchClerkId,char* pchPwd) {
    int inResult = VS_FAILURE;
    int inRecordSize;
    unsigned char ucUDTRec[SIZE_UDT_REC];
    FHANDLE hUDTFHandle;

    inRecordSize = 0;

    if ((hUDTFHandle = open(UDT_FILENAME, O_RDWR)) >= 0) {
        if (lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET) == (long) GEN_VER_SIZE)
            do {
                if ((inRecordSize = read(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC)) == SIZE_UDT_REC) {
                    if (strcmp(pchClerkId, szGetClerkIdFromStruct(ucUDTRec)) == 0) {
                        strlcpy(pchPwd, szGetClerkPasswdFromStruct(ucUDTRec), PASSWORD_SIZE);
                        if (close(hUDTFHandle) == 0)
                            return(VS_SUCCESS);
                    }
                }
            } while (inRecordSize == SIZE_UDT_REC);
        close(hUDTFHandle);
    }
    if (inRecordSize == 0)
        return(VS_ERR);
    return(inResult);
}

/********************************************************************************************* 
* @func int | inCheckUDTPwd |
*  
* Given password, verify if it exists 
*
*    
* @rdesc return values.
* @flag int    |  VS_ERR if match not found, VS_SUCCESS and clerk id, 
*                 VS_FAILURE if file errors
* @end
**********************************************************************************************/

int inCheckUDTPwd(char* pchPwd,char* pchClerkId) {
    int inResult = VS_FAILURE;
    int inRecordSize;
    unsigned char ucUDTRec[SIZE_UDT_REC];
    FHANDLE hUDTFHandle;

    /** /pdebug(("--inCheckUDTPwd--"));*/
    /** /pdebug(("pchPwd = %s", pchPwd));*/

    inRecordSize = 0;

    if ((hUDTFHandle = open(UDT_FILENAME, O_RDWR)) >= 0) {
        if (lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET) == (long) GEN_VER_SIZE)
            do {
                if ((inRecordSize = read(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC)) == SIZE_UDT_REC) {
                    if (strcmp(pchPwd, szGetClerkPasswdFromStruct(ucUDTRec)) == 0) {
                        /* returns clerk id associated with this password*/
                        strlcpy(pchClerkId, szGetClerkIdFromStruct(ucUDTRec), CLERK_ID_SIZE);
                        if (close(hUDTFHandle) == 0)
                            return(VS_SUCCESS);
                    }
                }
            } while (inRecordSize == SIZE_UDT_REC);
        close(hUDTFHandle);
    }
    if (inRecordSize == 0)
        return(VS_ERR);

    return(inResult);
}

/********************************************************************************************* 
* @func int | inCheckUDTClerkId |
*  
* Given clerk id, verify if it exists 
*
*    
* @rdesc return values.
* @flag int    |  VS_ERR if match not found, VS_SUCCESS  
*                 
* @end
**********************************************************************************************/

int inCheckUDTClerkId(char* pchClerkId) {
    char szLocalPwd[PASSWORD_SIZE + 1];

    return(inGetUDTPwd(pchClerkId, szLocalPwd));
}

/********************************************************************************************* 
* @func int | inGetUDTLogedIn |
*  
*  Input a clerk id. If clerk not logged in, asks for log in confirmation and
*  updates the UDT logged in flag. 
*
*    
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ESCAPE if cancelled, VS_ERR if match not found,
                  VS_FAILURE if file errors
*                 
* @end
**********************************************************************************************/

int inGetUDTLogedIn(TRANSACTION_OBJECT* pobTran,char* pchClerkId) {
    int inResult = VS_FAILURE;
    int inRecordSize;
    unsigned char ucUDTRec[SIZE_UDT_REC];
    FHANDLE hUDTFHandle;
    char chPassword[PASSWORD_SIZE + 1];
    int inOperationCode;  /* pj1 */
    char* chNull = 0;

    inOperationCode = inGGOpCode(); /* pj1 */

    inRecordSize = 0;

    if ((hUDTFHandle = open(UDT_FILENAME, O_RDWR)) >= 0) {
        if (lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET) == (long) GEN_VER_SIZE)
            do {
                if ((inRecordSize = read(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC)) == SIZE_UDT_REC) {
                    if (strcmp(pchClerkId, szGetClerkIdFromStruct(ucUDTRec)) == 0) {
                        /*  not currently logged in  */

                        if (inGetLogedInFromStruct(ucUDTRec) == VS_FALSE) {
                            if (inOperationCode == LOGOFF_OPERATION) {
                                vdDisplayMessageFromFile(ALREADY_LOGOFF_MSG, ERROR_MSG);
                                close(hUDTFHandle);
                                return(VS_ESCAPE);
                            }
                            /*  do not prompt user again if it's a login_operation */

                            if (inOperationCode != LOGIN_OPERATION) {
                                char szMessage[MESSAGE_SIZE + 1];

                                vdGetMessageFromFile(LOGON_MSG, szMessage);
                                if ((obDisp.inDO_ChooseOne(chNull, szMessage, pchGetMsg(YESNO_MSG5x), 2, NULL_PFI)) != KEY_YES) {
                                    close(hUDTFHandle);
                                    return(VS_ESCAPE);
                                }
                            }
                            do {
                                if ((inResult = inGetPassword(PASSWD_MSG15, chPassword)) == VS_ESCAPE) {
                                    close(hUDTFHandle);
                                    return(VS_ESCAPE);
                                }
                                if ((inResult = strcmp(chPassword, szGetClerkPasswdFromStruct(ucUDTRec))) != 0) {
                                    vdDisplayMessageFromFile(INV_PWD_ERR_MSG21, ERROR_MSG);
                                    vdClearStatusMessage(); /* pj1 */
                                }
                            } while (inResult != 0);

                            vdSetLogedInFromStruct(VS_TRUE, ucUDTRec);
                            /* move back file ptr and update the record */
                            if (lseek(hUDTFHandle, ((long) (SIZE_UDT_REC)) * -1, SEEK_CUR) < 0) {
                                close(hUDTFHandle);
                                return(VS_ERR);
                            }
                            inResult = write(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC);
                            if (inResult != SIZE_UDT_REC)     /* Error writing to file */ {
                                close(hUDTFHandle);
                                return(VS_ERR);
                            }

                            break; /* log on successful */
                        } else {
                            /*  currently logged in  */
                            if (inOperationCode == LOGIN_OPERATION) {
                                vdDisplayMessageFromFile(ALREADY_LOGIN_MSG, ERROR_MSG);
                                close(hUDTFHandle);
                                return(VS_ESCAPE);
                            }

                            if (inOperationCode == LOGOFF_OPERATION) {
                                if ((obDisp.inDO_ChooseOne(chNull, pchGetMsg(CONFIRM_MSG15), pchGetMsg(YESNO_MSG5x), 2, NULL_PFI)) != KEY_YES) {
                                    close(hUDTFHandle);
                                    return(VS_ESCAPE);
                                }

                                vdSetLogedInFromStruct(VS_FALSE, ucUDTRec);
                                if (lseek(hUDTFHandle, ((long) (SIZE_UDT_REC)) * -1, SEEK_CUR) < 0) /* move back file ptr */ {
                                    close(hUDTFHandle);
                                    return(VS_ERR);
                                }
                                inResult = write(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC); /* update rec */
                                if (inResult != SIZE_UDT_REC)     /* Error writing to file */ {
                                    close(hUDTFHandle);
                                    return(VS_ERR);
                                }
                                if (close(hUDTFHandle) < 0)
                                    return(VS_ERR);

                                /* -- we found the clerk and successfully logged him off...
                                                        ...lets get out of here otherwise we will report
                                                        an incorrect Invalid Clerk ID error -- */
                                return(VS_SUCCESS);
                            }

                            if (close(hUDTFHandle) == 0)
                                return(VS_SUCCESS);
                        } /*  end of ELSE */
                    }
                }
            } while (inRecordSize == SIZE_UDT_REC);
        close(hUDTFHandle);
    }
    /* Get out in Clear key press */
    if (act_kbd_pending_test(KEY_CANCEL))
        return (VS_ESCAPE);
    if (inRecordSize <= 0)
        return(VS_ERR);

    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func int | inResetUDTLogedIn |
*  
*  
*  Clear log flag in UDT file. 
*
*    
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ESCAPE if cancelled, VS_ERR
*       
*                 
* @end
**********************************************************************************************/

int inResetUDTLogedIn(void) {
    int inResult = VS_SUCCESS;
    int inRecordSize,
    inADTRecordSize;
    unsigned char ucUDTRec[SIZE_UDT_REC];
    FHANDLE hUDTFHandle;
    ADT_REC srADTRec;
    char szADTFileName[FILENAME_SIZE + 1] ;
    FHANDLE hADTFHandle;
    int inNoADTForClerk;
    int inResetAll = VS_FALSE;


    inCombineActiveClerks();
    /* This is useing temp ADT file created in CombineActiveClerks  */
    vdGetMessageFromFile(ADT_FILENAME, szADTFileName);

    /* open ADT table */
    if ((hADTFHandle = open(szADTFileName, O_RDWR)) < 0)
        if ((hADTFHandle = open(szADTFileName, O_CREAT | O_RDWR)) < 0)
            return(VS_ERR);

    if ((hUDTFHandle = open(UDT_FILENAME, O_RDWR)) >= 0) {
        if (lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET) == (long) GEN_VER_SIZE)
            do {
                lseek(hADTFHandle, 0L, SEEK_SET);
                if ((inRecordSize = read(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC)) == SIZE_UDT_REC) {
                    inNoADTForClerk = VS_TRUE;
                    do {
                        if ((inADTRecordSize = read(hADTFHandle, (char *) &srADTRec, SIZE_ADT_REC)) == SIZE_ADT_REC) {
                            if (strcmp(srADTRec.szClerkId, szGetClerkIdFromStruct(ucUDTRec)) == 0) {
                                inNoADTForClerk = VS_FALSE;
                                break;
                            }
                        }
                    } while (inADTRecordSize == (int) SIZE_ADT_REC);
                    if (inNoADTForClerk == VS_TRUE) {
                        vdSetLogedInFromStruct(VS_FALSE, ucUDTRec);
                        vdSetCashTipFromStruct(0L, ucUDTRec);
                        /* move back file ptr for update */
                        if (lseek(hUDTFHandle, ((long) (SIZE_UDT_REC)) * -1, SEEK_CUR) < 0) {
                            close(hUDTFHandle);
                            return(VS_ERR);
                        }
                        inResult = write(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC);
                        if (inResult != SIZE_UDT_REC)     /* Error writing to file */ {
                            close(hUDTFHandle);
                            return(VS_ERR);
                        } else
                            inResult = VS_SUCCESS;
                    }
                }
            } while (inRecordSize == SIZE_UDT_REC);
        close(hUDTFHandle);
    }
    close(hADTFHandle);

    return(inResult);
}

/********************************************************************************************* 
* @func int | inUpdateUDTRecord |
*  
*  
*  Updates the specific UDT Record with given totals 
*
*    
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*       
*                 
* @end
**********************************************************************************************/

int inUpdateUDTRecord(char* szClrk,long lnCashTip) {
    unsigned char ucUDTRec[SIZE_UDT_REC];
    FHANDLE hUDTFHandle;
    int inResult;

    if (inGetClerkMode() == NO_CLERK)
        return(VS_SUCCESS);



    if ((hUDTFHandle = open(UDT_FILENAME, O_RDWR)) < 0)
        return(VS_ERR);

    if (lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET) != (long) GEN_VER_SIZE) {
        close(hUDTFHandle);
        return(VS_ERR);
    }
    do {
        inResult = read(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC);
        if (inResult == 0)
            break;                 /* End of file reached */
        if (inResult != SIZE_UDT_REC)             /* Read error, get out */ {
            close(hUDTFHandle);
            return(VS_ERR);
        }

        if (strcmp(szClrk, szGetClerkIdFromStruct(ucUDTRec)) == 0) {
            vdSetCashTipFromStruct(lnCashTip, ucUDTRec);

            if (lseek(hUDTFHandle, ((long) (SIZE_UDT_REC)) * -1, SEEK_CUR) < 0) {
                close(hUDTFHandle);
                return(VS_ERR);
            }
            inResult = write(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC);
            if (inResult != SIZE_UDT_REC)     /* Error writing to file */ {
                close(hUDTFHandle);
                return(VS_ERR);
            }
            if (close(hUDTFHandle) < 0)
                return(VS_ERR);

            return(VS_SUCCESS);
        }
    } while (1);
    close(hUDTFHandle);

    return(VS_ERR);
}

/********************************************************************************************* 
* @func int |inUpdateUTTRecord |
*  
*  
* Updates the specific UTT Record with given totals
*
*    
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*       
*                 
* @end
**********************************************************************************************/

int inUpdateUTTRecord(TRANSACTION_OBJECT* pobTran,char* szClrk,int inCnt,long lnBase,long lnTax,long lnTip,long lnCsh,long lnSurcharge,long lnTipSale) {
    char szUTTFileName[FILENAME_SIZE + 1];
    DB_FILE srDBFPtr;
    UTT_REC srUTTRec;
    UTT_KEY srUTTKey;
    int inKeyIdx = 0;
    long lnRecNum = 0L;
    int inRetVal;

    /* pdebug(("--inUpdateUTTRecord--")); */

    if (inGetClerkMode() == NO_CLERK)
        return(VS_SUCCESS);

    strlcpy(srUTTKey.szClerkID, szClrk, CLERK_ID_SIZE);

    vdGetUTTFileName(szUTTFileName);

    if (inOpenUTT(&srDBFPtr, szUTTFileName) != VS_SUCCESS) {
        vdSGErrorMessage(UTT_ERR_MSG21);
        return(VS_ERR);
    }

    /* If record does not exist then append new record */

    inRetVal = inKeyReadUTT(&srDBFPtr, &srUTTRec, &srUTTKey, KEY_SVR_ID, &lnRecNum);

    if (inRetVal == VS_ERR) {
        lnRecNum = DB_APPEND;
        strlcpy(srUTTKey.szClerkID, szClrk, CLERK_ID_SIZE);
    } else if (inRetVal == VS_ESCAPE) {
        inCloseUTT(&srDBFPtr);
        return (VS_ERR);
    }

    srUTTRec.inNetCount += inCnt;
    srUTTRec.lnNetTotAmt += lnBase + lnTax + lnTip + lnCsh + lnSurcharge;
    srUTTRec.lnNetBaseAmt += lnBase;
    srUTTRec.lnNetTaxAmt += lnTax;
    srUTTRec.lnNetTipAmt += lnTip ;
    srUTTRec.lnNetCashBack += lnCsh;
    srUTTRec.lnNetSurcharge += lnSurcharge;
    srUTTRec.lnTipSale += lnTipSale;

    /* Save the information */

    if (db_write(&srDBFPtr, &srUTTKey, (char *) &srUTTRec, SIZE_UTT_REC, lnRecNum) < 0) {
        vdSGErrorMessage(UTT_ERR_MSG21);
        inCloseUTT(&srDBFPtr);
        return(VS_ERR);
    }
    inRetVal = inCloseUTT(&srDBFPtr);

    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func int |inSetUTTAmount |
*  
*  
* Calculate and set amount fields in active UTT record
*
*    
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*       
*                 
* @end
**********************************************************************************************/

int inSetUTTAmount(TRANSACTION_OBJECT* pobTran) {
    int inCount;
    int inTranCode;
    char szClerkId[CLERK_ID_SIZE + 1];
    long lnBaseAmt,
    lnTipAmt,
    lnTaxAmt,
    lnCashBack,
    lnSurcharge,
    lnTipSale;
    long lnTotalOfTransAmount,
    lnTipAmount,
    lnOldTotalTransAmount,
    lnOldTipAmount;
    int inType;
    long lnTaxCBSurCharge;   /* Sum of Tax + CashBack + SurCharge */

    /* pdebug(("--inSetUTTAmount--")); */

    inCount = 0;
    lnBaseAmt = 0L;
    lnTaxAmt = 0L;
    lnTipAmt = 0L;
    lnCashBack = 0L;
    lnSurcharge = 0L;
    lnTipSale = 0L;

    lnOldTotalTransAmount = 0L;
    lnOldTipAmount = 0L;
    lnTotalOfTransAmount = 0L;
    lnTipAmount = 0L;

    if (inGetClerkMode() == NO_CLERK)
        return(VS_SUCCESS);

    inTranCode = inGGCode();

    lnOldTotalTransAmount = pobTran->srBRec.lnOldTotalTransactionAmount;
    lnTotalOfTransAmount = pobTran->srBKey.lnTotalOfTransactionAmount;
    lnTipAmount = pobTran->srBRec.lnTipAmount;
    lnOldTipAmount = pobTran->srBRec.lnOldTipAmount;
    lnTaxCBSurCharge = (pobTran->srBRec.lnTaxAmount + pobTran->srBRec.lnCashBackAmount + pobTran->srBRec.lnSChgAmount);

    strlcpy(szClerkId, pobTran->srBKey.szClerkId, CLERK_ID_SIZE);

    inType = pobTran->srBRec.inType;
    if (inType == ADJUST_CREDIT_TRAN)
        inType = CREDIT_TRAN;
    if (inType == ADJUST_DEBIT_TRAN)
        inType = DEBIT_TRAN;

    if ((inType == DEBIT_TRAN) && !(pobTran->fAdjust || pobTran->fTipAdjust)) {
        /** /pdebug(("case01")); */
        inCount = 1;
        lnTaxAmt = pobTran->srBRec.lnTaxAmount;
        lnCashBack = pobTran->srBRec.lnCashBackAmount;
        lnSurcharge = pobTran->srBRec.lnSChgAmount;

        lnTipAmt = pobTran->srBRec.lnTipAmount;
        lnBaseAmt = (lnTotalOfTransAmount - lnTipAmt - lnTaxAmt - lnCashBack - lnSurcharge);

        if (pobTran->srBRec.lnTipAmount > 0)
            lnTipSale = lnBaseAmt;
        else
            lnTipSale = 0L;
    }

    if ((inType == CREDIT_TRAN) && !(pobTran->fAdjust || pobTran->fTipAdjust)) {
        /** /pdebug(("case02"));*/
        inCount = 1;
        lnTaxAmt = -pobTran->srBRec.lnTaxAmount;
        lnCashBack = -pobTran->srBRec.lnCashBackAmount;
        lnSurcharge = -pobTran->srBRec.lnSChgAmount;

        lnTipAmt = -pobTran->srBRec.lnTipAmount;
        lnTotalOfTransAmount *= -1L;
        lnBaseAmt = (lnTotalOfTransAmount - lnTipAmt - lnTaxAmt - lnCashBack - lnSurcharge);

        if (pobTran->srBRec.lnTipAmount > 0)
            lnTipSale = lnBaseAmt;
        else
            lnTipSale = 0L;
    }

    if ((inType == DEBIT_TRAN) && (pobTran->fAdjust || pobTran->fTipAdjust)) {
        /** /pdebug(("case03"));*/
        inCount = 0;
        lnTaxAmt = 0L;     /* can not adjust tax, cashback, surcharge */
        lnCashBack = 0L;
        lnSurcharge = 0L;

        if (pobTran->fTaxAdjust)
            lnTaxAmt = (pobTran->srBRec.lnTaxAmount - pobTran->lnOldTaxAmount);
        if (pobTran->fAdjust) {
            lnBaseAmt = (lnTotalOfTransAmount - pobTran->srBRec.lnTaxAmount) - (lnOldTotalTransAmount - pobTran->lnOldTaxAmount);

            if (pobTran->srBRec.lnTipAmount == pobTran->srBRec.lnOldTipAmount)
                lnTipAmt = 0L;
            else {
                lnTipAmt = (pobTran->srBRec.lnTipAmount - pobTran->srBRec.lnOldTipAmount);
                lnBaseAmt -= lnTipAmt;
            }

            if (lnTipAmount > 0)
                lnTipSale = lnBaseAmt;
            else
                lnTipSale = 0L;
        } else if (pobTran->fTipAdjust) {
            lnTipAmt = (lnTipAmount - lnOldTipAmount);
            lnBaseAmt = 0L;

            /* the case of both lnTipAmount and lnOldTipAmount > 0  */
            /*  or both = 0             has no effect on lnTipSale  */
            if (lnTipAmount == 0 && lnOldTipAmount > 0)
                lnTipSale = -(lnOldTotalTransAmount - lnOldTipAmount - lnTaxCBSurCharge);

            if (lnTipAmount > 0 && lnOldTipAmount == 0)
                lnTipSale = lnTotalOfTransAmount - lnTipAmount - lnTaxCBSurCharge;
        }
    }

    if ((inType == CREDIT_TRAN) && (pobTran->fAdjust || pobTran->fTipAdjust)) {
        /** /pdebug(("case04"));*/

        inCount = 0;
        lnTaxAmt = 0L;     /* can not adjust tax, cashback, surcharge */
        lnCashBack = 0L;
        lnSurcharge = 0L;

        if (pobTran->fTaxAdjust)
            lnTaxAmt = -(pobTran->srBRec.lnTaxAmount - pobTran->lnOldTaxAmount);
        if (pobTran->fAdjust) {
            lnBaseAmt = -((lnTotalOfTransAmount - pobTran->srBRec.lnTaxAmount) - (lnOldTotalTransAmount - pobTran->lnOldTaxAmount));

            if (pobTran->srBRec.lnTipAmount == pobTran->srBRec.lnOldTipAmount)
                lnTipAmt = 0L;
            else {
                lnTipAmt = -(pobTran->srBRec.lnTipAmount - pobTran->srBRec.lnOldTipAmount);
                lnBaseAmt -= lnTipAmt;
            }

            if (lnTipAmount > 0)
                lnTipSale = lnBaseAmt;
            else
                lnTipSale = 0L;
        } else if (pobTran->fTipAdjust) {
            lnTipAmt = -(lnTipAmount - lnOldTipAmount);
            lnBaseAmt = 0L;

            /* the case of both lnTipAmount and lnOldTipAmount > 0  */
            /*  or both = 0             has no effect on lnTipSale  */
            if (lnTipAmount == 0 && lnOldTipAmount > 0)
                lnTipSale = (lnOldTotalTransAmount - lnOldTipAmount - lnTaxCBSurCharge);

            if (lnTipAmount > 0 && lnOldTipAmount == 0)
                lnTipSale = -(lnTotalOfTransAmount - lnTipAmount - lnTaxCBSurCharge);
        }
    }

    if (pobTran->srBKey.fVoided)
        return(inUpdateUTTRecord(pobTran, szClerkId, -1, (lnBaseAmt * -1L), (lnTaxAmt * -1L), (lnTipAmt * -1L), (lnCashBack * -1L), (lnSurcharge * -1L), (lnTipSale * -1L)));
    else
        return(inUpdateUTTRecord(pobTran, szClerkId, inCount, lnBaseAmt, lnTaxAmt, lnTipAmt, lnCashBack, lnSurcharge, lnTipSale));
}

/********************************************************************************************* 
* @func int |inGetUDTTotRecs |
*  
*  
* Get the number of records in the User Definition Table
*
*    
* @rdesc return values.
* @flag int    |  Number of records in the UDT
*       
*                 
* @end
**********************************************************************************************/

int inGetUDTTotRecs() {
    long lnSize;
    char szUDTFileName[FILENAME_SIZE + 1] ;

    strcpy(szUDTFileName, UDT_FILENAME);
    lnSize = lnGetFileSize(szUDTFileName);

    return((int) ((lnSize - GEN_VER_SIZE) / (long) SIZE_UDT_REC));
}

/********************************************************************************************* 
* @func int |inInitUDTAmounts |
*  
*  Initializes the Tip and Total amounts after each settlement.
* 
*    
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*       
*                 
* @end
**********************************************************************************************/

int inInitUDTAmounts(void) {
    FHANDLE hUDTClerkFHandle;

    if (inGetClerkMode() == NO_CLERK)
        return(VS_SUCCESS);


    if ((hUDTClerkFHandle = open(UDT_FILENAME, O_TRUNC | O_CREAT | O_RDWR)) >= 0)
        return (VS_ERR);
    close(hUDTClerkFHandle);

    return(VS_SUCCESS);
}

/********************************************************************************************* 
* @func int |inResetUTT |
*  
*  Clear out the UTT User totals table
* 
*    
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*       
*                 
* @end
**********************************************************************************************/

int inResetUTT(TRANSACTION_OBJECT* pobtran) {
    int inRetval;
    char szFileName[FILENAME_SIZE + 1];
    DB_FILE srDBFPtr;

    /** /pdebug(("--inResetUTT--"));*/

    vdGetUTTFileName(szFileName);
    if ((db_open(&srDBFPtr, szFileName, SIZE_UTT_KEY, (O_TRUNC | O_CREAT | O_RDWR))) < 0)
        return(VS_ERR);
    inRetval = inCloseUTT(&srDBFPtr);

    return (inRetval);
}

/********************************************************************************************* 
* @func int |vdGetADTFileName |
*  
*  Returns the corresponding ADT file name based on the host
*  being accessed.
* 
*    
* @rdesc return values.
* @flag int    |  ADT (active definition table) file name
*       
*                 
* @end
**********************************************************************************************/

void vdGetADTFileName(char* pchADTFileName) {
    /** /pdebug(("--vdGetADTFileName--")); */

    sprintf(pchADTFileName, "ADT%02d%02d.DAT", inGetBatchIndex(), inGetMerchantNum());

    /** /pdebug(("ADT=%s",pchADTFileName)); */
} /* end of vdGetADTFileName() */


/********************************************************************************************* 
* @func int |inInsertADTEntry |
*  
*  Inserts an entry for each clerk who has activity in the
*  current batch.  Clerks are added to this temporary file
*  in numeric sorted order.  If clerk already exists in the
*  file, just return.  
* 
*    
* @rdesc return values.
* @flag int    |  VS_FAILURE, VS_SUCCESS
*       
*                 
* @end
**********************************************************************************************/


int inInsertADTEntry(char* pchClerkId,char* szADTFileName) {
    int inRetVal = VS_FAILURE;
    ADT_REC srADTRec;
    FHANDLE hADTFHandle;
    int inRecordSize;
    char szPadNewClkId[CLERK_ID_SIZE + 1];
    char szPadCurrentClkId[CLERK_ID_SIZE + 1];
    BOOL fQuitLoop = FALSE;

    /** /pdebug(("--inInsertADTEntry--"));*/

    strcpy(szPadNewClkId, pchClerkId);

    /* alphanumeric sort */
    /* pad (szPadNewClkId, szPadNewClkId, SPACE, CLERK_ID_SIZE, LEFT);  */

    /* numeric sort */
    pad(szPadNewClkId, szPadNewClkId, '0', CLERK_ID_SIZE, RIGHT);

    if ((hADTFHandle = open(szADTFileName, O_RDWR)) < 0) {
        if ((hADTFHandle = open(szADTFileName, O_CREAT | O_RDWR)) < 0)
            return(VS_ERR);
        else {
            memset(srADTRec.szClerkId, '\0', CLERK_ID_SIZE);
            strlcpy(srADTRec.szClerkId, pchClerkId, CLERK_ID_SIZE);
            inRetVal = (int) insert(hADTFHandle, (char *) &srADTRec, SIZE_ADT_REC);
            close(hADTFHandle);
            if (inRetVal == SIZE_ADT_REC)
                return(VS_SUCCESS);
            else
                return(VS_ERR);
        }
    }


    do {
        inRecordSize = read(hADTFHandle, (char *) &srADTRec, SIZE_ADT_REC);
        if (inRecordSize == SIZE_ADT_REC) {
            strcpy(szPadCurrentClkId, srADTRec.szClerkId);

            /* alphanumeric sort */
            /* pad (szPadCurrentClkId, szPadCurrentClkId,SPACE, CLERK_ID_SIZE, LEFT);  */

            /* numeric sort */
            pad(szPadCurrentClkId, szPadCurrentClkId, '0', CLERK_ID_SIZE, RIGHT);

            /* if clerk already exists, exit */
            if (strcmp(szPadNewClkId, szPadCurrentClkId) == 0) {
                close(hADTFHandle);
                return(VS_SUCCESS);
            }

            /* if new ClerkId is < ClerkId then we need to backup */
            if (strcmp(szPadNewClkId, szPadCurrentClkId) < 0) {
                lseek(hADTFHandle, ((long) SIZE_ADT_REC) * -1, SEEK_CUR);
                fQuitLoop = TRUE;
            }
        } else
            fQuitLoop = TRUE;
    } while (fQuitLoop != TRUE);

    memset(srADTRec.szClerkId, '\0', CLERK_ID_SIZE);
    strlcpy(srADTRec.szClerkId, pchClerkId, CLERK_ID_SIZE);
    if (insert(hADTFHandle, (char *) &srADTRec, SIZE_ADT_REC) == SIZE_ADT_REC)
        inRetVal = VS_SUCCESS;
    else
        inRetVal = VS_ERROR;

    close(hADTFHandle);
    return(inRetVal);
} /* end of inInsertADTEntry () */

/********************************************************************************************* 
* @func int |inResetADT |
*  
*  
*  Clear the ADT file.
*      
* @rdesc return values.
* @flag int    |  VS_ERR, VS_SUCCESS
*       
*                 
* @end
**********************************************************************************************/


int inResetADT(void) {
    char szADTFileName[FILENAME_SIZE + 1] ;

    /** /pdebug(("--inResetADT--")); */

    vdGetADTFileName(szADTFileName);

    inRemoveFile(szADTFileName);

    return (VS_SUCCESS);
} /* end of inResetADT() */


/********************************************************************************************* 
* @func int |inGetADTTotRecs |
*  
*  
*  Return the number of records in the Active Definition File
*  Is the number of currently active clerks
*
*
* @rdesc return values.
* @flag int    |  Number of clerks in the Active table
*       
*                 
* @end
**********************************************************************************************/


int inGetADTTotRecs() {
    long lnSize;
    char szADTFileName[FILENAME_SIZE + 1] ;

    vdGetADTFileName(szADTFileName);

    lnSize = lnGetFileSize(szADTFileName);

    return((int) ((lnSize) / (long) SIZE_ADT_REC));
} /* end of inGetADTTotRecs() */



/********************************************************************************************* 
* @func int |inVerifyClerkPwd |
*  
*  
*  Validate clerk input password
*  
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR
*       
*                 
* @end
**********************************************************************************************/


int inVerifyClerkPwd(char* pchPwd,char* pchClerkId) {
    int inResult = VS_FAILURE;
    int inRecordSize;
    unsigned char ucUDTRec[SIZE_UDT_REC];
    FHANDLE hUDTFHandle;

    /** /pdebug(("--inVerifyClerkPwd--"));*/
    /** /pdebug(("pchPwd = %s", pchPwd));*/

    inRecordSize = 0;

    if ((hUDTFHandle = open(UDT_FILENAME, O_RDWR)) >= 0) {
        if (lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET) == (long) GEN_VER_SIZE)
            do {
                if ((inRecordSize = read(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC)) == SIZE_UDT_REC) {
                    if (strcmp(pchClerkId, szGetClerkIdFromStruct(ucUDTRec)) == 0)
                        if (strcmp(pchPwd, szGetClerkPasswdFromStruct(ucUDTRec)) == 0) {
                            if (close(hUDTFHandle) == 0)
                                return(VS_SUCCESS);
                        }
                }
            } while (inRecordSize == SIZE_UDT_REC);
        close(hUDTFHandle);
    }
    if (inRecordSize == 0)
        return(VS_ERR);

    return(inResult);
}


/********************************************************************************************* 
* @func int |inModifyPassword |
*  
*  
* Update clerk password
*  
*
*
* @rdesc return values.
* @flag int    |  VS_ERR on file error
*       
*                 
* @end
**********************************************************************************************/

int inModifyPassword(char* szClerkID,char* szPassword) {
    int inResult;
    unsigned char ucUDTRec[SIZE_UDT_REC];
    FHANDLE hUDTFHandle;

    pdebug(("--inModifyPassword--"));

    if ((hUDTFHandle = open(UDT_FILENAME, O_RDWR)) < 0)
        return(VS_ERR);

    if (lseek(hUDTFHandle, (long) GEN_VER_SIZE, SEEK_SET) != (long) GEN_VER_SIZE) {
        close(hUDTFHandle);
        return(VS_ERR);
    }
    do {
        inResult = read(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC);
        if (inResult == 0)
            break;                
        if (inResult != (int) SIZE_UDT_REC)   /* Read error, get out */ {
            close(hUDTFHandle);
            return(VS_ERR);
        }

        if (strcmp(szClerkID, szGetClerkIdFromStruct(ucUDTRec)) == 0) {
            vdSetClerkPasswdFromStruct(szPassword, ucUDTRec);
            if (lseek((int) hUDTFHandle, ((long) (SIZE_UDT_REC)) * -1, SEEK_CUR) < 0L) {
                close(hUDTFHandle);
                return(VS_ERR);
            }
            inResult = write(hUDTFHandle, (char *) ucUDTRec, SIZE_UDT_REC);
            if (inResult != (int) SIZE_UDT_REC)     /* Error writing to file */ {
                close(hUDTFHandle);
                return(VS_ERR);
            }
            if (close(hUDTFHandle) < 0)
                return(VS_ERR);
            break;
        }
    } while (1);

    close(hUDTFHandle);

    return(inResult);
}

/********************************************************************************************* 
* @func int |inValidateClerkPwd |
*  
*  
* Get a password from the user and validate this password
* against all the passwords stored in UDT file.
*  
*
*
* @rdesc return values.
* @flag int    |  VS_SUCCESS, VS_ERR 
*       
*                 
* @end
**********************************************************************************************/

int inValidateClerkPwd(char* szInputPwd) {
    /**pdebug(("--inValidateClerkPwd--"));**/

    if (inCheckUDTPwd(szInputPwd, srGDS.szClerkIdFromPW) < VS_SUCCESS) {
        if (inValidateSuperPwd(szInputPwd) != VS_SUCCESS) {
            if (inValidateManagerPwd(szInputPwd) != VS_SUCCESS) {
                vdSGErrorMessage(INV_PWD_ERR_MSG21);
                return(VS_ERR);
            }
        }
    } else
        vdSetPWSecurityLevel(ACCESS_WITH_CLERK_PASSWORD);

    return(VS_SUCCESS);
}


#undef VSUDT_C

